/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: data_source.c
 *
 * Purpose: wraper png decode and raw type convert as a data source for kfa maker
 *
 * Developer:
 *   wen.gu , 2022-03-06
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "data_source.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include "rgb_to_nv12.h"

#include <iostream>
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#if 0
#define LOG_TRACE() std::cout <<"[" << __FUNCTION__ << "][" << __LINE__<< "]===>:" << std::endl
#else
#define LOG_TRACE()
#endif
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/
static bool is_need_raw_convert(kfa_raw_type raw_type)
{
    return raw_type == kfa_raw_type::NV12; /** todo process other type */
}

static uint32_t get_image_size(uint16_t img_width, uint16_t img_height, kfa_raw_type raw_type)
{
    switch (raw_type)
    {
    case kfa_raw_type::RGBA32: return img_width * img_height * 4; break;
    case kfa_raw_type::RGB24:  return img_width * img_height * 3; break;
    case kfa_raw_type::YUV420:
    case kfa_raw_type::NV12:
        return img_width * img_height * 3 / 2;
    break;
    default:
        break;
    }

    return 0;
}

static uint32_t get_rgb_step(kfa_raw_type raw_type)
{
    switch (raw_type)
    {
    case kfa_raw_type::RGBA32: return 4; break;
    case kfa_raw_type::RGB24:  return 3; break;
    break;
    default:
        break;
    }

    return 0;
}

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

DataSource::DataSource()
{

}

DataSource::~DataSource()
{

}

bool DataSource::setOutputType(kfa_raw_type raw_type)
{
    raw_type_ = raw_type;
    return true;
}

uint8_t* DataSource::getData(const std::string& img_file, uint16_t& img_width, uint16_t& img_height,  uint32_t& pic_total_size)
{
    LOG_TRACE();
    int req_pic_n = get_rgb_step(raw_type_);
    int pic1_n = 0;
    unsigned char* pic1_data = stbi_load(img_file.c_str(), (int*)&img_width, (int*)&img_height, &pic1_n, req_pic_n);
    LOG_TRACE();
    if (!pic1_data)
    {
        return nullptr;
    }
    LOG_TRACE();
    pic_total_size = get_image_size(img_width, img_height, raw_type_);
    if (!pic_total_size)
    {
        return nullptr;
    }
    LOG_TRACE();
    uint8_t* data_buf = new uint8_t[pic_total_size];

    if (is_need_raw_convert(raw_type_))
    {
        LOG_TRACE();        
        rgb_to_nv12(pic1_data, pic1_n, img_width, img_height, data_buf);            
    }      
    else
    {
        LOG_TRACE();
        memcpy(data_buf, pic1_data, pic_total_size);
    }
    //std::cout <<"req pic n: " << req_pic_n << ", pic n: " << pic1_n << ", w: " << img_width << ", h: " << img_height << ", size: " << pic_total_size  << ", data: " << static_cast<const void *>(pic1_data) << ", out: "<< static_cast<const void *>(data_buf) << std::endl;
    LOG_TRACE();

    free(pic1_data);
    LOG_TRACE();

    return data_buf;    
}
